package com.das.mechanic_base.widget;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.main.X3SelectBrandAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.bean.main.MapCarBrandBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.List;

public class X3BottomHomeDialog extends X3BaseBottomSheetDialog implements View.OnClickListener, X3SelectBrandAdapter.IOnClickSelect {

    TextView tv_cancel;
    TextView tv_work;
    TextView tv_appoint;
    RecyclerView rl_brand;
    Button btn_back;
    Button btn_next;

    private X3SelectBrandAdapter brandAdapter;
    private long brandId;
    /**
     * 0 全部类型
     * 1 施工单
     * 2 预约单
     */
    private int workStatus;

    private String brandName;
    /**
     * 一：判断是否是点击筛选里面的重置按钮之后请求的接口
     * 二：只有点击重置按钮这个状态才会变成ture，否则点击类型和品牌的话都为false
     * 三：点击重置按钮刷新数据之后，确定按钮的文字只显示确定
     */
    private boolean isReset;//

    public X3BottomHomeDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_work = findViewById(R.id.tv_work);
        tv_appoint = findViewById(R.id.tv_appoint);
        rl_brand = findViewById(R.id.rl_brand);
        btn_back = findViewById(R.id.btn_back);
        btn_next = findViewById(R.id.btn_next);

        tv_cancel.setOnClickListener(this);
        tv_work.setOnClickListener(this);
        tv_appoint.setOnClickListener(this);
        btn_back.setOnClickListener(this);
        btn_next.setOnClickListener(this);

        rl_brand.setLayoutManager(new GridLayoutManager(mContext, 3));
        brandAdapter = new X3SelectBrandAdapter(mContext);
        rl_brand.setAdapter(brandAdapter);
        brandAdapter.setiOnClickSelect(this);

    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_home_bottom_dialog;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.tv_work) {// TODO 施工单类型
            tv_work.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_select_item));
            tv_work.setTextColor(Color.parseColor("#0077ff"));

            tv_appoint.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_unselect_item));
            tv_appoint.setTextColor(Color.parseColor("#666666"));

            workStatus = 1;
            changeBtn(true);
            isReset = false;
            if (iOnSelectHome != null) {
                iOnSelectHome.iOnSelectNo(workStatus, brandId);
            }
        } else if (id == R.id.tv_appoint) {// TODO 预约单类型
            tv_appoint.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_select_item));
            tv_appoint.setTextColor(Color.parseColor("#0077ff"));

            tv_work.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_unselect_item));
            tv_work.setTextColor(Color.parseColor("#666666"));

            workStatus = 2;
            changeBtn(true);
            if (iOnSelectHome != null) {
                iOnSelectHome.iOnSelectNo(workStatus, brandId);
            }
            isReset = false;
        } else if (id == R.id.btn_back) {
            changeBtn(false);
            btn_next.setEnabled(true);
            btn_next.setText(mContext.getString(R.string.x3_affirm_info));
            btn_next.setBackgroundColor(Color.parseColor("#0077ff"));
            tv_work.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_unselect_item));
            tv_work.setTextColor(Color.parseColor("#666666"));
            tv_appoint.setBackground(mContext.getDrawable(R.drawable.x3_shape_idea_unselect_item));
            tv_appoint.setTextColor(Color.parseColor("#666666"));

            if (brandAdapter != null) {
                brandAdapter.changeReset();
            }

            isReset = true;

            if (iOnSelectHome != null) {
                iOnSelectHome.iOnSelectNo(0, 0);
            }
        } else if (id == R.id.btn_next) {
            dismiss();
            if (iOnSelectHome != null) {
                if (isReset) {
                    iOnSelectHome.iOnSelectReset();
                } else {
                    iOnSelectHome.iOnSelectHome(workStatus, brandId, brandName);
                }
            }
        }
    }

    IOnSelectHome iOnSelectHome;

    public void setiOnSelectHome(IOnSelectHome iOnSelectHome) {
        this.iOnSelectHome = iOnSelectHome;
    }

    public interface IOnSelectHome {
        void iOnSelectHome(long workStatus, long brandId, String brandName);

        void iOnSelectReset();

        void iOnSelectNo(long workStatus, long brandId);
    }

    @Override
    public void show() {
        super.show();
        btn_next.setText(mContext.getString(R.string.x3_affirm_info));
    }

    public void changeData(String select, List<MapCarBrandBean> mapCarBrandBeans, String selectBrand) {
        if (select.equals(tv_work.getText().toString())) {
            tv_work.performClick();
        }
        if (select.equals(tv_appoint.getText().toString())) {
            tv_appoint.performClick();
        }

        if (brandAdapter != null) {
            brandAdapter.changeSelect(selectBrand, mapCarBrandBeans);
        }

        if (!X3StringUtils.isListEmpty(mapCarBrandBeans)) {
            for (MapCarBrandBean carBrandBean : mapCarBrandBeans) {
                if (selectBrand.equals(carBrandBean.getName())) {
                    brandId = carBrandBean.getId();
                    brandName = selectBrand;
                    changeBtn(true);
                }
            }
        }
    }

    public void showHomeNums(int size) {
        if (isReset) {
            btn_next.setText(mContext.getString(R.string.x3_affirm_info));
        } else {
            switch (size) {
                case 0:
                    btn_next.setText(mContext.getString(R.string.x3_affirm_info) + "(" + mContext.getString(R.string.x3_home_no_select) + ")");
                    break;
                case 1:
                    btn_next.setText(mContext.getString(R.string.x3_affirm_info) + String.format(mContext.getString(R.string.x3_home_filte_one), size));
                    break;
                default:
                    btn_next.setText(mContext.getString(R.string.x3_affirm_info) + String.format(mContext.getString(R.string.x3_home_filte_more), size));
                    break;
            }
        }
    }

    @Override
    public void iOnClickSelectBrand(MapCarBrandBean carBrandBean) {
        isReset = false;
        changeBtn(true);
        this.brandId = carBrandBean.getId();
        brandName = carBrandBean.getName();

        if (iOnSelectHome != null) {
            iOnSelectHome.iOnSelectNo(workStatus, brandId);
        }
    }

    public void changeBtn(boolean isClick) {
        btn_back.setEnabled(isClick);
        btn_next.setEnabled(isClick);

        btn_back.setTextColor(isClick ? Color.parseColor("#0077ff") : Color.parseColor("#C7C7C7"));
        btn_next.setBackgroundColor(isClick ? Color.parseColor("#0077ff") : Color.parseColor("#D5D5D5"));

    }
}
